package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.exception.LogicException;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.query.PageResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.utils.UserContext;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
@Transactional
public class IEmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void insert(Employee employee, Long[] ids) {
        employeeMapper.insert(employee);

        if(ids!=null&&ids.length>0){
            for(Long id:ids){
                employeeMapper.queryForRoles(employee.getId(), id);
            }
        }
    }

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);

        if(ids!=null&&ids.length>0){
            for(Long id:ids){
                employeeMapper.queryForRoles(employee.getId(), id);
            }
        }
    }

    @Override
    public Employee selectOne(Long id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    @Override
    public List<Employee> selectAll() {
        List<Employee> list = employeeMapper.selectAll();
        return list;
    }

    @Override
    public PageInfo<Employee> query(QueryObject queryObject) {
        PageHelper.startPage(queryObject.getCurrentPage(), queryObject.getPageSize());
        List<Employee> list = employeeMapper.queryForList(queryObject);
        return new PageInfo(list);
    }

    @Override
    public Employee login(String username, String password) {
        if(!(username!=null&&username.trim().length()>0)){
            throw new LogicException("用户名不能为空");
        }
        if(!(password!=null&&password.trim().length()>0)){
            throw new LogicException("密码不能为空");
        }
        Employee employee=employeeMapper.selectByUsernameAndPassword(username,password);
        if(employee==null){
            throw new LogicException("用户或密码错误");
        }
        return employee;
    }

    @Override
    public void judageUpdate(String oldPassword, String newPassword) {
        //1、判断用户输入的旧密码是否与现在用户登录的密码相同，若不同，抛出异常信息
        //先获取当前用户的信息
        Employee user = UserContext.getCurrentUser();
        String password = user.getPassword();
        System.out.println(password);
        //查询现在用户登录的密码
        if(!password.equals(oldPassword)){
            throw new RuntimeException("原密码错误");
        }
        //2、将用户输入的新密码存储到数据库当中
        user.setPassword(newPassword);
        employeeMapper.updatePwd(user);
    }

    @Override
    public void updatePwd(String newPassword) {
        //获取到当前用户信息
        Employee employee = UserContext.getCurrentUser();
        //将新密码设置到该用户上，并储存到数据库中
        employee.setPassword(newPassword);
        employeeMapper.updatePwd(employee);
    }

    @Override
    public void batchDelete(Long[] ids) {
        if(ids!=null&&ids.length>0){
            employeeMapper.batchDelete(ids);
        }
    }

    @Override
    public Employee selectName(String name) {
        if(name!=null&&name.length()>0){
            Employee employee=employeeMapper.selectName(name);
            return employee;
        }else {
            return null;
        }

    }

    @Override
    public void updateStatus(Long id) {
            //先查询出该用户的信息
            Employee employee = employeeMapper.selectByPrimaryKey(id);
            boolean status = employee.isStatus();
            employee.setStatus(!status);
            employeeMapper.updateStatus(employee);
    }
}
